package practica6;

import framework.IBasicState;

public class StateB implements IBasicState {

    private int[][] state = { { 7, 3, 5 }, { 8, 9, 2 }, { 4, 1, 3 } };
    private final int BALVACIA = 9;
    private StateB parent;
    private int posX;
    private int posY;

    public StateB() {
	posX = 1;
	posY = 1;
    }

    @Override
    public IBasicState getParent() {
	return this.parent;
    }

    @Override
    public boolean isFinal() {
	return isSorted();
    }

    public boolean balVaciaIsCenter() {
	return state[1][1] == BALVACIA;
    }

    public boolean balVaciaIsLeftSuperior() {
	return state[0][0] == BALVACIA;
    }
    
    public boolean balVaciaIsLeftInf() {
	return state[2][0] == BALVACIA;
    }
    public boolean balVaciaIsLeft() {
	return state[1][0] == BALVACIA;
    }
    public boolean balVaciaIsRigthSuperior() {
	return state[0][2] == BALVACIA;
    }
    public boolean balVaciaIsRigthInf() {
	return state[2][2] == BALVACIA;
    }
    public boolean balVaciaIsRigth() {
	return state[1][2] == BALVACIA;
    }
    public boolean balVaciaIsMiddleSup() {
	return state[0][1] == BALVACIA;
    }
    public boolean balVaciaIsMiddleInf() {
	return state[2][1] == BALVACIA;
    }
    
    public StateB clone() {
	StateB nuevaMatriz = new StateB();
	for (int i = 0; i < 3; i++) {
	    for (int j = 0; j < 3; j++) {
		nuevaMatriz.state[i][j] = state[i][j];
	    }
	}
	nuevaMatriz.parent = this.parent;
	nuevaMatriz.posX = this.posX;
	nuevaMatriz.posY = this.posY;
	return nuevaMatriz;
    }

    private boolean isSorted() {
	boolean res = true;
	int aux = 0;
	for (int i = 0; i < 3; i++) {
	    for (int j = 0; j < 3; j++) {
		if (aux > state[i][j])
		    res = false;
		else
		    aux = state[i][j];
	    }
	}
	return res;
    }

    @Override
    public boolean repOk() {
	return false;
    }

    @Override
    public void setParent(IBasicState state) {
	this.parent = (StateB) state;
    }

    @Override
    public boolean success() {
	return isSorted();
    }

    /**
     * @param x
     *            columna
     * @param y
     *            fila
     */
    public void moveUp(int x, int y) {
	int aux = state[x][y - 1];
	this.state[x][y - 1] = state[x][y];
	state[x][y] = aux;
	posX = x;
	posY = y - 1;
    }

    public void moveDown(int x, int y) {
	int aux = state[x][y + 1];
	this.state[x][y + 1] = state[x][y];
	state[x][y] = aux;
	posX = x;
	posY = y + 1;
    }

    public void moveLeft(int x, int y) {
	int aux = state[x - 1][y];
	this.state[x - 1][y] = state[x][y];
	state[x][y] = aux;
	posX = x - 1;
	posY = y;
    }

    public void moveRigth(int x, int y) {
	int aux = state[x + 1][y];
	this.state[x + 1][y] = state[x][y];
	state[x][y] = aux;
	posX = x + 1;
	posY = y;
    }

    public int posX (){
	return this.posX;
    }
    public int posY (){
	return this.posY;
    }

    public int  stateI (int x , int y){
	return state[x][y];
    }
}
